// ConfigGUIDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ConfigGUI.h"
#include "ConfigGUIDlg.h"
#include "ShooterDlg.h"
#include "TrainDlg.h"
#include "MovPlayCtrl.h"
#include "..\multimedia\video.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

extern void CenterRectangle(CRect, CRect*);

/////////////////////////////////////////////////////////////////////////////
// CConfigGUIDlg dialog


CConfigGUIDlg::CConfigGUIDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CConfigGUIDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CConfigGUIDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void CConfigGUIDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CConfigGUIDlg)
	DDX_Control(pDX, IDB_TRAINSUB, m_btnTrain);
	DDX_Control(pDX, IDB_SHOOTER, m_btnShooter);
	DDX_Control(pDX, IDB_STOP, m_btnStop);
	DDX_Control(pDX, IDB_REPLAY, m_btnReplay);
	DDX_Control(pDX, IDB_PLAY, m_btnPlay);
	DDX_Control(pDX, IDB_PAUSE, m_btnPause);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CConfigGUIDlg, CDialog)
	//{{AFX_MSG_MAP(CConfigGUIDlg)
	ON_BN_CLICKED(IDB_SHOOTER, OnShooter)
	ON_BN_CLICKED(IDB_EXIT, OnExit)
	ON_BN_CLICKED(IDB_TRAINSUB, OnTrainsub)
	ON_WM_SHOWWINDOW()
	ON_BN_CLICKED(IDB_PAUSE, OnPause)
	ON_BN_CLICKED(IDB_STOP, OnStop)
	ON_BN_CLICKED(IDB_PLAY, OnPlay)
	ON_BN_CLICKED(IDB_REPLAY, OnReplay)
	ON_WM_CLOSE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CConfigGUIDlg message handlers


void CConfigGUIDlg::OnShooter() 
{
	// TODO: Add your control notification handler code here
	if (FALSE == m_pShooterDlg->Create(CShooterDlg::IDD, NULL))
	{
		TRACE0("Creation of Shooter dialog failed.\n");
		return;
	}
	CRect rcParent, rcMy;
	GetWindowRect(&rcParent);
	m_pShooterDlg->GetWindowRect(&rcMy);
	CenterRectangle(rcParent, &rcMy);
	m_pShooterDlg->MoveWindow(rcMy);
	m_pShooterDlg->ShowWindow(SW_SHOW);
	m_btnShooter.EnableWindow(FALSE);
}

void CConfigGUIDlg::OnExit() 
{
	// TODO: Add your control notification handler code here
	CWnd* wndParent = GetOwner();
	DestroyWindow();
	wndParent->DestroyWindow();
}

void CConfigGUIDlg::OnTrainsub() 
{
	// TODO: Add your control notification handler code here
	if (FALSE == m_pTrainDlg->Create(CTrainDlg::IDD, NULL))
	{
		TRACE0("Creation of Train subject dialog failed.\n");
		return;
	}
	CRect rcParent, rcMy;
	GetWindowRect(&rcParent);
	m_pTrainDlg->GetWindowRect(&rcMy);
	CenterRectangle(rcParent, &rcMy);
	m_pTrainDlg->MoveWindow(rcMy);
	m_pTrainDlg->ShowWindow(SW_SHOW);
	m_btnTrain.EnableWindow(FALSE);
}

BOOL CConfigGUIDlg::PreTranslateMessage(MSG* pMsg) 
{
	// TODO: Add your specialized code here and/or call the base class
	ASSERT(NULL != m_hAccelTable);
	if (WM_KEYDOWN == pMsg->message && VK_ESCAPE == pMsg->wParam)
	{
		SendMessage(WM_CLOSE, 0, 0L);
		return TRUE;
	}
    int status =
		::TranslateAccelerator(AfxGetApp()->m_pMainWnd->GetSafeHwnd(),
								m_hAccelTable, pMsg);
	if (0 == status)
		return CDialog::PreTranslateMessage(pMsg);
	else
		return TRUE;
}

BOOL CConfigGUIDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	m_hAccelTable = ::LoadAccelerators(AfxGetInstanceHandle(),
						MAKEINTRESOURCE(IDA_CONFIG));

	CMovPlayCtrl* player = (CMovPlayCtrl*)GetDlgItem(IDC_MOVPLAY);
	ASSERT(player && player->IsKindOf(RUNTIME_CLASS(CMovPlayCtrl)));
	Init_Video(AfxGetInstanceHandle(), player->GetSafeHwnd());

	m_pShooterDlg = new CShooterDlg;
	m_pShooterDlg->SetDlgParent(this);

	m_pTrainDlg = new CTrainDlg;
	m_pTrainDlg->SetDlgParent(this);
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

BOOL CConfigGUIDlg::DestroyWindow() 
{
	// TODO: Add your specialized code here and/or call the base class
	Destroy_Video();
	if (::IsWindow(m_pShooterDlg->GetSafeHwnd()))
		m_pShooterDlg->DestroyWindow();
	delete m_pShooterDlg;
	if (::IsWindow(m_pTrainDlg->GetSafeHwnd()))
		m_pTrainDlg->DestroyWindow();
	delete m_pTrainDlg;
	return CDialog::DestroyWindow();
}

void CConfigGUIDlg::OnShowWindow(BOOL bShow, UINT nStatus) 
{
	CDialog::OnShowWindow(bShow, nStatus);
	
	// TODO: Add your message handler code here
	Play_Video(bShow);
	m_btnPlay.EnableWindow(FALSE);
	m_btnReplay.EnableWindow(FALSE);
	m_btnPause.EnableWindow();
	m_btnStop.EnableWindow();
}

void CConfigGUIDlg::OnPause() 
{
	// TODO: Add your control notification handler code here
	Pause_Video();
	m_btnPause.EnableWindow(FALSE);
	m_btnPlay.EnableWindow();
}

void CConfigGUIDlg::OnStop() 
{
	// TODO: Add your control notification handler code here
	Stop_Video();
	m_btnStop.EnableWindow(FALSE);
	m_btnReplay.EnableWindow();
	m_btnPause.EnableWindow(FALSE);
}

void CConfigGUIDlg::OnPlay() 
{
	// TODO: Add your control notification handler code here
	Play_Video(TRUE);
	m_btnPlay.EnableWindow(FALSE);
	m_btnPause.EnableWindow();
	m_btnReplay.EnableWindow(FALSE);
	m_btnStop.EnableWindow();
}

void CConfigGUIDlg::OnReplay() 
{
	// TODO: Add your control notification handler code here
	Replay_Video();
	m_btnReplay.EnableWindow(FALSE);
	m_btnStop.EnableWindow();
	m_btnPause.EnableWindow();
}

void CConfigGUIDlg::OnClose() 
{
	// TODO: Add your message handler code here and/or call default
	DestroyWindow();
	CDialog::OnClose();
}
